package com.credithc.signature.manager.controller;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.signature.common.ro.facade.ReSignData;
import com.credithc.signature.common.ro.manager.JobRequestRo;
import com.credithc.signature.common.service.JobLogService;
import com.credithc.signature.common.utils.DateUtil;
import com.credithc.signature.common.vo.manager.RequestLogVo;
import com.credithc.signature.manager.client.SignatureFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年06⽉29⽇<br/>
 * @version：v1.0.0<br>
 */
@Slf4j
@RestController
@RequestMapping("/manager/scs/v2")
public class JobController {

    @Autowired
    MessageFactory messageFactory;

    @Autowired
    JobLogService jobLogService;

    @Autowired
    SignatureFeignClient feignClient;

    /**
     * 查询所有
     *
     * @param jobRequestRo
     * @return
     */
    @PostMapping("/joblogs/list")
    public Message queryAll(@RequestBody JobRequestRo jobRequestRo) {
        try {
            buildTimeRange(jobRequestRo);
            PageInfo<RequestLogVo> pageInfo = jobLogService.queryAll(jobRequestRo);
            Map<String, PageInfo> map = new HashMap<>(16);
            map.put("pageInfo", pageInfo);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
        } catch (Exception e) {
            log.error("查询未处理成功数据时发生异常", e);
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "查询未处理成功数据时发生异常");
        }
    }

    /**
     * 重签
     *
     * @param jobRequestRo
     * @return
     */
    @ResponseBody
    @PostMapping("/signature/reTry")
    public Message reTry(@RequestBody JobRequestRo jobRequestRo) {
        try {
            ReSignData reSignData = new ReSignData();
            reSignData.setNeedSign(jobRequestRo.getNeedSign());
            reSignData.setChannelNo(jobRequestRo.getChannelNo());
            reSignData.setCallBackUrl(jobRequestRo.getCallBackUrl());
            reSignData.setRequestSn(jobRequestRo.getRequestSn());
            reSignData.setTemplateNo(jobRequestRo.getTemplateNo());
            reSignData.setRequestData(jobRequestRo.getRequestData());
            feignClient.resign(reSignData);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, "发送成功！");
        } catch (Exception e) {
            log.error("发送重签请求失败", e);
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "发送失败！");
        }
    }

    /**
     * 设置查询时间范围
     *
     * @param jobRequestRo
     */
    private static void buildTimeRange(JobRequestRo jobRequestRo) {
        if (Objects.isNull(jobRequestRo.getCreateTime())) {
            Date nowDate = new Date();
            jobRequestRo.setCreateTime(DateUtil.getFirstTimeOfDay(nowDate));
            jobRequestRo.setStartTime(DateUtil.getFirstTimeOfDay(nowDate));
            jobRequestRo.setEndTime(DateUtil.getLastTimeOfDay(nowDate));
        }
    }
}
